<script setup>
import List from '../components/List.vue'
import ClusterNamespaceSelector from '../components/ClusterNamespaceSelector.vue'
import { getSecretListHandler as getListHandler, deleteSecretHandler as deleteHandler } from '../../api/secret.js'
import { ref, toRefs, reactive, computed } from 'vue'
import DialogYaml from '../components/DialogYaml.vue' // codeMirror 从DialogYaml组件引用
import { obj2Yaml } from '../../utils/utils'
import GenericOptions from '../components/GenericOptions.vue'

const data = reactive({
  items: [],
  clusterId: "",
  namespace: "",
  deleteItem: "",
  yamlContent: ""
})

// 定义查询pod列表函数
const getList = () => {
  getListHandler({clusterId: data.clusterId, namespace: data.namespace}).then(
    (res) => {
      data.items = res.data.data.items
      console.log("Secret列表",data.items)
    }
  )
}

// 回调函数传给ClusterNamespaceSelector组件
const rollback = (clusterId,namespace) => {
  data.clusterId = clusterId
  data.namespace = namespace
  getList()
}
const search = ref('')
const filterTableData = computed(() =>
  data.items.filter(
    (item) =>
      !search.value ||
      item.metadata.name.toLowerCase().includes(search.value.toLowerCase())
  )
)

const showDetail = ref(false)
const detail = (row) => {
  const item = JSON.parse(JSON.stringify(row))
  const itemtemp = { // yaml补上apiVersion字段
    "apiVersion": "v1",
    "kind": "Secret",
    "metadata": item.metadata,
    "data": item.data,
    "immutable": item.immutable
  }
  const yamlData = obj2Yaml(itemtemp) // 当前数据转成yaml
  data.yamlContent = yamlData
  showDetail.value = true

}
const { yamlContent, clusterId } = toRefs(data)

const handlerChange = (value) => {
  console.log(value)
}
</script>

<template>
  <List title="Secret列表">
    <template #header-options>
      <ClusterNamespaceSelector @namespaceChangedRollback="rollback">
      </ClusterNamespaceSelector>
      <el-input v-model="search" size="small" placeholder="搜索" style="width: 140px; margin-left: 10px; height: 32px;"/>
    </template>
    <template #main>
      <el-table
      border
      height="480"
      :data="filterTableData"
      style="width: 100%"
      :default-sort="{prop:'metadata.creationTimestamp', order:'descending'}"
      >
        <el-table-column fixed prop="" align="center" label="名称">
          <template #default="scope">
            <el-button link type="primary" @click="detail(scope.row)">{{ scope.row.metadata.name }}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="type" align="center" label="类型" width="320"/>
        <el-table-column prop="immutable" align="center" label="只读" width="120"/>

        <el-table-column prop="metadata.namespace" align="center" label="命名空间" width="120"/>
        <el-table-column fixed="right" align="center" label="操作" width="320" >
          <template #default="scope">
            <GenericOptions
            v-on:delete-rollback="getList"
            :disabled="scope.row.metadata.deletionTimestamp != undefined"
            resource-type="Secret"
            :name="scope.row.metadata.name"
            :namespace="scope.row.metadata.namespace"
            :cluster-id="clusterId"
            ></GenericOptions>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </List>
  <DialogYaml
  :cluster-id="data.clusterId"
  :namespace="data.namespace"
  :yaml-content="yamlContent"
  :show-dialog="showDetail"
  @close-dialog-handler="(val) => {showDetail = val }"
  >
  </DialogYaml>
  <!-- <el-dialog
    destroy-on-close 
    v-model="showDetail" 
    width="70%">
      <template #header="{titleId,titleClass}">
        <div class="my-header">
          <div :id="titleId" :class="titleClass">
            <el-button type="primary" size="large" title="资源所在集群">
              <i class="iconfont icon-k8s" style="font-size: 20px"/>
              <span style="font-size: 20px;margin-left: 10px;">{{ data.clusterId }}</span>
            </el-button>
            <el-button type="primary" size="large" title="资源所在空间">
              <i class="iconfont icon-k8s" style="font-size: 20px"/>
              <span style="font-size: 20px;margin-left: 10px;">{{ data.namespace }}</span>
            </el-button>
          </div>
        </div>
      </template>
      <CodeMirror
      v-model="yamlContent"
      >

      </CodeMirror>
  </el-dialog> -->
</template>

<style scoped>
</style>
